<?php


namespace Tools;

class WebResult
{
    public static function JsonResult($data = null, $msg = "Success", $code = 0)
    {
        header('Content-Type:application/json');
        header('Access-Control-Allow-Origin:*');
        header('Access-Control-Allow-Headers:*');
        header('Access-Control-Allow-Credentials:true');

        if (empty($data)) {
            echo json_encode(array(
                "code" => $code,
                "msg" => $msg
            ));
        } else {
            echo json_encode(array(
                "code" => $code,
                "msg" => $msg,
                "data" => $data
            ));
        }
        die();
    }

    public static function JsonSuccess($msg = "Success", $code = 0)
    {
        self::JsonResult(null, $msg, $code);
    }

    public static function JsonFail($msg = "Fail", $code = 1)
    {
        self::JsonResult(null, $msg, $code);
    }

    public static function JsonEmpty()
    {
        self::JsonFail('can_not_be_empty', 1);
    }
}